package com.wdz.currency_query.utils;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wdz.utils.PageUtil;
import com.wdz.utils.ParamsUtil;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Param extends PageUtil {
    public Keywords keywords;
    public Compare compare;

    public Keywords getKeywords() {
        return keywords;
    }

    public void setKeywords(Keywords keywords) {
        this.keywords = keywords;
    }

    public Compare getCompare() {
        return compare;
    }

    public void setCompare(Compare compare) {
        this.compare = compare;
    }

    /**
     * 关键词查询
     */
    public class Keywords{
        public String k_cloum;
        public String k_value;
        /**
         * 附加条件
         */
        private ParamsUtil additionalMap;
        /**
         * 排序规则
         */
        private Map<String,Object> orderBy;
        /**
         * 模糊查询类型
         * 0：%value% 1:value% 2:%value 3:==value
         */
        private int k_type;

        public Map<String, Object> getOrderBy() {
            return orderBy;
        }

        public void setOrderBy(Map<String, Object> orderBy) {
            this.orderBy = orderBy;
        }

        public String getK_cloum() {
            return k_cloum;
        }

        public void setK_cloum(String k_cloum) {
            this.k_cloum = k_cloum;
        }

        public String getK_value() {
            return k_value;
        }

        public void setK_value(String k_value) {
            this.k_value = k_value;
        }

        public int getK_type() {
            return k_type;
        }

        public void setK_type(int k_type) {
            this.k_type = k_type;
        }

        public ParamsUtil getAdditionalMap() {
            return additionalMap;
        }

        public void setAdditionalMap(ParamsUtil additionalMap) {
            this.additionalMap = additionalMap;
        }
        public Wrapper getCompareSentence() {
            if(k_type>3 || k_type<0){
                newRuntimeParamException();
            }
            Wrapper wrapper = new EntityWrapper();
            if(k_type == 3){
                if(k_cloum != null) {
                    StringBuilder sql = new StringBuilder(k_cloum);
                    sql.append("= {0}");
                    wrapper.where(sql.toString(), k_value);
                }
            }else {
                SqlLike sqlLike = null;
                switch (k_type) {
                    case 0:
                        sqlLike = SqlLike.DEFAULT;
                        break;
                    case 1:
                        sqlLike = SqlLike.RIGHT;
                        break;
                    case 2:
                        sqlLike = SqlLike.LEFT;
                        break;
                }
                wrapper.like(k_cloum,k_value,sqlLike);
            }
            if(additionalMap != null){
                wrapper.where(additionalMap.getSqlWhere(),additionalMap.getSqlValues());
            }
            if(orderBy != null){
                boolean b = true;
                if("false".equals(orderBy.get("isAsc"))){
                    b = false;
                }
                wrapper.orderBy(orderBy.get("cloum").toString(),b);
            }

            return wrapper;
        }
    }

    /**
     * 比较大小查询
     */
    public class Compare{
        private String cloum;
        /**
         * 查询类型
         * 0：>= <=查询,1:>=查询,2:<=查询,3:>= <查询,4:> <=查询,5:> <查询,6:<查询,7:>查询
         */
        private int type;

        public String getCloum() {
            return cloum;
        }

        public void setCloum(String cloum) {
            this.cloum = cloum;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

            private String l_value;

            public String getL_value() {
                return l_value;
            }

            public void setL_value(String l_value) {
                this.l_value = l_value;
            }

            private String s_value;

            public String getS_value() {
                return s_value;
            }

            public void setS_value(String s_value) {
                this.s_value = s_value;
            }

        public Wrapper getCompareSentence() {
            StringBuilder sql = new StringBuilder();
            List<Object> list = new ArrayList<Object>();

            if (type == 0 || type == 3 || type == 4 || type == 5){
                if (!(l_value != null && s_value != null)) {
                    newRuntimeParamException();
                }
            sql.append(cloum);
            if (type == 0) {
                sql.append(" >={0} and ");
                sql.append(cloum);
                sql.append(" <={1}");
            } else if (type == 3) {
                sql.append(" >={0} and ");
                sql.append(cloum);
                sql.append(" <{1}");
            } else if (type == 4) {
                sql.append(" >{0} and ");
                sql.append(cloum);
                sql.append(" <= {1}");
            } else if (type == 5) {
                sql.append(" >{0} and");
                sql.append(cloum);
                sql.append(" <{1}");
            }
            list.add(l_value);
            list.add(s_value);
        }else if(type == 1 || type == 6) {
                if (l_value == null) {
                    newRuntimeParamException();
                }
                sql.append(cloum);
                if (type == 1) {
                    sql.append(" >={0}");
                } else if (type == 6) {
                    sql.append(" >{0}");
                }
                list.add(l_value);
            }else if(type == 2 || type == 7){
                    if(s_value == null){
                        newRuntimeParamException();
                    }
                    sql.append(cloum);
                    if(type == 2) {
                        sql.append(" <={0}");
                    }else if(type == 7){
                        sql.append(" <{0}");
                    }
                    list.add(s_value);
            }
            return new EntityWrapper().where(sql.toString(),list.toArray());
        }

    }

    private void newRuntimeParamException(){
        throw new RuntimeException("参数不能为空");
    }
}
